package mastermind.database;
import java.util.*;
import java.io.Serializable;

@SuppressWarnings("serial")
public class GameUserTable implements Serializable {
	ArrayList<GameUserEntry> users;
	
	GameUserTable() {
		users = new ArrayList<GameUserEntry>();
	}
	
	/**
	 * adds a user
	 * @param GameUserEntry Object holding users details.
	 */
	void addUser(GameUserEntry user) {
		users.add(user);
	}
	
	/**
	 * returns a user
	 * @param getUser User to search for
	 * @return The user object or null on failure
	 */
	GameUserEntry getUser(String userName) {
		GameUserEntry tempUser;
		for(int x = 0; x < users.size(); x++) {
			tempUser = users.get(x);
			if(tempUser.getUserName().equals(userName)) {
				return tempUser;
			}
		}
		return null;
	}
	
	/**
	 * removes a user
	 * @param userName User to remove
	 * @return false on failure
	 */
	boolean removeUser(String userName) {
		GameUserEntry tempUser;
		for(int x = 0; x < users.size(); x++) {
			tempUser = users.get(x);
			if(tempUser.getUserName() == userName) {
				users.remove(x);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * checks login
	 * @param userName username
	 * @param password password
	 */
	boolean checkLogin(String userName,String password) {
		GameUserEntry tempUser;
		for(int x = 0; x < users.size(); x++) {
			tempUser = users.get(x);
			if(tempUser.getUserName().equals(userName) && tempUser.getPassword().equals(password)) {
				return true;
			}
		}
		return false;
	}
}
